package com.qingke.oms.factory;

import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;

import com.qingke.oms.command.Command;
import com.qingke.oms.command.shop.AddToShoppingCartCommand;
import com.qingke.oms.command.shop.DeleteFromShoppingCartCommand;
import com.qingke.oms.command.shop.DeliveryCommand;
import com.qingke.oms.command.shop.EmptyShoppingCartCommand;
import com.qingke.oms.command.shop.LeaveShopCommand;
import com.qingke.oms.command.shop.OffShelfCommand;
import com.qingke.oms.command.shop.OnShelfCommand;
import com.qingke.oms.command.shop.PlaceOrderCommand;
import com.qingke.oms.command.shop.SetPriceCommand;
import com.qingke.oms.command.shop.ShopHelpCommand;
import com.qingke.oms.command.shop.ShopStatusCommand;
import com.qingke.oms.command.shop.ViewGoodsCommand;
import com.qingke.oms.command.shop.ViewShoppingCartCommand;
import com.qingke.oms.pojo.Shop;
import com.qingke.oms.pojo.User;

public class ShopCommandFactory extends UserCommandFactory {
	private static Map<CommandCode,Class<? extends Command>> shopMap=new HashMap<>();
	static{
		shopMap.put(CommandCode.ADD, AddToShoppingCartCommand.class);
		shopMap.put(CommandCode.DELETE, DeleteFromShoppingCartCommand.class);
		shopMap.put(CommandCode.DELIVERY, DeliveryCommand.class);
		shopMap.put(CommandCode.EMPTY, EmptyShoppingCartCommand.class);
		shopMap.put(CommandCode.LEAVE, LeaveShopCommand.class);
		shopMap.put(CommandCode.OFFSHELF, OffShelfCommand.class);
		shopMap.put(CommandCode.ONSHELF, OnShelfCommand.class);
		shopMap.put(CommandCode.ORDER, PlaceOrderCommand.class);
		shopMap.put(CommandCode.STATUS, ShopStatusCommand.class);
		shopMap.put(CommandCode.GOODS, ViewGoodsCommand.class);
		shopMap.put(CommandCode.CART, ViewShoppingCartCommand.class);
		shopMap.put(CommandCode.HELP, ShopHelpCommand.class);
		shopMap.put(CommandCode.SETPRICE, SetPriceCommand.class);
	}
	
	private Shop shop=null;
	
	public ShopCommandFactory(User user,Shop shop) {
		super(user);
		this.shop=shop;
	}
	
	
	public Shop getShop() {
		return shop;
	}


	@Override
	public Command buildCommand(CommandCode cmdCode) {
		Class<? extends Command> commandClass=null;
		commandClass=shopMap.get(cmdCode);
		if(commandClass==null)
			return super.buildCommand(cmdCode);
		
		Command command=null;
		try {
			command=commandClass.getConstructor(User.class,Shop.class).newInstance(this.getUser(),this.getShop());
		} catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException
				| NoSuchMethodException | SecurityException e) {
			e.printStackTrace();
		}
		return command;
	}

}
